/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 */

/**
 * @addtogroup STM32
 */
/*@{*/

#include <board.h>
#include <rtthread.h>

#ifdef RT_USING_DFS
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

#ifdef RT_USING_LWIP
#include <lwip/sys.h>
#include <lwip/api.h>
#include <netif/ethernetif.h>
#endif

#include "components.h"

extern void rt_platform_init(void);
extern void realtouch_ui_init(void);
void rt_filesystem_init(void)
{
	rt_bool_t mount_flag = RT_FALSE;
	if(dfs_mount("flash0", "/", "elm", 0, 0) == 0)
	{
		mount_flag = RT_TRUE;
	}
	else
	{
		rt_kprintf("flash0 mount to / failed! format and try again!\n");

		/* format filesystem */
		dfs_mkfs("elm","flash0");

		/*re-try mount*/
		if(dfs_mount("flash0", "/", "elm", 0, 0) == 0)
		{
			mount_flag = RT_TRUE;
		}
	}
	if (mount_flag == RT_TRUE)
	{
		rt_kprintf("flash0 mount to / \n");
		
		/*download resource from www.rt-thread.org*/
		{
		//	resource_download();
		}
#if (RT_DFS_ELM_USE_LFN != 0) && (defined RT_DFS_ELM_CODE_PAGE_FILE)
		{
			extern void ff_convert_init(void);
			ff_convert_init();
		}
#endif
		if (dfs_mount("sd0", "/SD", "elm", 0, 0) == 0)
		{
			rt_kprintf("sd0 mount to /SD \n");
		}
		else
		{
			rt_kprintf("sd0 mount to /SD failed!\n");
		}
	}
	else
	{
		rt_kprintf("flash0 mount to / failed! \n");
	}
}

void rt_init_thread_entry(void* parameter)
{


	rt_components_init();

	rt_platform_init();

	rt_filesystem_init();
	
#ifdef RT_USING_RTGUI
	realtouch_ui_init();
#endif
}



int rt_application_init()
{
    rt_thread_t init_thread;


#if (RT_THREAD_PRIORITY_MAX == 32)
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   1024, 8, 20);
#else
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   1024, 80, 20);
#endif

    if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);



    return 0;
}

